﻿using Microsoft.EntityFrameworkCore;
using MyTask.Domain.Commands;
using MyTask.Domain.Commands.Handlers;
using MyTask.Domain.Queries;
using MyTask.Domain.Queries.Handlers;
using MyTask.Infrastructure.Context;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Xunit;

namespace MyTask.Test
{
   public class TestTask
    {
        private static string connectionString = "Data Source=DESKTOP-MIB8DI4\\SQLEXPRESS;Initial Catalog=MyTask;Integrated Security=true;";
        private DbContextOptions<MyTaskContext> options = new DbContextOptionsBuilder<MyTaskContext>()
            .UseSqlServer(connectionString)
            .Options;

        [Fact]
        public void Test1()
        {
            MyTaskContext context = new MyTaskContext(options);
            TaskInfoCommandHandler taskInfoCommandHandler = new TaskInfoCommandHandler(context);
            TaskInfoCommand command = new TaskInfoCommand();
            command.TaskName = "测试任务1";
            command.TaskDescrible = "测试任务1";
            command.StartTime =DateTime.Now;
            command.EndTime = DateTime.Now.AddDays(1);
            command.TaskResult = "完成的好";
            Assert.True(taskInfoCommandHandler.AddTaskInfo(command));
        }

        [Fact]
        public void Test2()
        {
            MyTaskContext context = new MyTaskContext(options);
            TaskInfoQueryHandler taskInfoQueryHandler = new TaskInfoQueryHandler(context);
            GetTaskInfoByIdQuery query = new GetTaskInfoByIdQuery();
            query.TaskId =2;
            var result = taskInfoQueryHandler.GetTaskInfoById(query);
            Assert.NotNull(result);
        }

        [Fact]
        public void Test3()
        {
            MyTaskContext context = new MyTaskContext(options);
            TaskInfoQueryHandler taskInfoQueryHandler = new TaskInfoQueryHandler(context);
            GetTaskInfosByUserQuery query = new GetTaskInfosByUserQuery();
            query.UserId = 3;
            var result = taskInfoQueryHandler.GetTaskInfosByUser(query);
            Assert.True(result.Any());
        }

        [Fact]
        public void Test4()
        {
            MyTaskContext context = new MyTaskContext(options);
            TaskInfoQueryHandler taskInfoQueryHandler = new TaskInfoQueryHandler(context);
            GetSharedTasksByUserQuery query = new GetSharedTasksByUserQuery();
            query.UserId = 3;
            var result = taskInfoQueryHandler.GetSharedTasksByUser(query);
            Assert.True(result.Any());
        }

        [Fact]
        public void Test5()
        {
            MyTaskContext context = new MyTaskContext(options);
            TaskInfoCommandHandler taskInfoCommandHandler = new TaskInfoCommandHandler(context);
            TaskInfoCommand command = new TaskInfoCommand();
            command.TaskId = 6;
            command.TaskName = "测试任务1";
            command.TaskDescrible = "测试任务1";
            command.StartTime = DateTime.Now;
            command.EndTime = DateTime.Now.AddDays(1);
            command.TaskResult = "完成的好1";
            command.IsComplete = true;
            Assert.True(taskInfoCommandHandler.UpdateTaskInfo(command));
        }
    }
}
